package me.xcmc.org.api;

import me.xcmc.org.StarsItem;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Owner {
    public static boolean isBind(ItemStack is){
        boolean b = false;
        if (is.hasItemMeta()){
            if (Objects.requireNonNull(is.getItemMeta()).hasLore()){
                if (Objects.requireNonNull(is.getItemMeta().getLore()).toString().contains(StarsItem.a)){
                    b = true;
                }
            }
        }
        return b;
    }
    public static boolean isOwner(ItemStack is, Player p){
        boolean b = false;
        if (!isBind(is)){
            if (Objects.requireNonNull(Objects.requireNonNull(is.getItemMeta()).getLore()).toString().contains(StarsItem.b.replace("<player>",p.getName()))){
                b = true;
            }
        }
        return b;
    }
    public static void setBind(ItemStack is, Player p){
        if (isBind(is)){
            ArrayList<String> il = new ArrayList<>();
            for (String s : Objects.requireNonNull(Objects.requireNonNull(is.getItemMeta()).getLore())){
                if (s.contains(StarsItem.a)){
                    s = s.replace(StarsItem.a,StarsItem.b.replace("<player>",p.getName()));
                }
                il.add(s);
            }
            ItemMeta im = is.getItemMeta();
            im.setLore(il);
            is.setItemMeta(im);
        }
    }
    public static boolean isStarsItem(ItemStack is){
        boolean b = false;
        if (is != null){
            if (is.getType() != Material.AIR){
                if (is.hasItemMeta()){
                    if (Objects.requireNonNull(is.getItemMeta()).hasDisplayName()){
                        for (String s : StarsItem.it.getKeys(false)){
                            if (Objects.equals(StarsItem.it.getString(s + ".Material","PAPER"), is.getType().toString())){
                                if (Objects.equals(StarsItem.it.getString(s + ".name"), is.getItemMeta().getDisplayName())){
                                    b = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        return b;
    }

    public static String getStarsItem(ItemStack is){
        String z = null;
        for (String s : StarsItem.it.getKeys(false)){
            if (Objects.equals(StarsItem.it.getString(s + ".name"), Objects.requireNonNull(is.getItemMeta()).getDisplayName())){
                z = s;
                break;
            }
        }
        return z;
    }
    public static ItemStack getItem(String s){
        ItemStack is = null;
        if (StarsItem.it.get(s) != null){
            FileConfiguration c = StarsItem.it;
            is = new ItemStack(Objects.requireNonNull(Material.getMaterial(Objects.requireNonNull(c.getString(s + ".Material", "PAPER")))));
            for (String e : Objects.requireNonNull(c.getConfigurationSection(s + ".Enchantment")).getKeys(false)){
                is.addUnsafeEnchantment(Objects.requireNonNull(Enchantment.getByName(e)),c.getInt(s + ".Enchantment." + e));
            }
            ItemMeta im = is.getItemMeta();
            assert im != null;
            im.setDisplayName(c.getString(s + ".name","§4错误"));
            if (c.getStringList(s + ".lore").size() > 0){
                List<String> l = c.getStringList(s + ".lore");
                if (!l.toString().contains(StarsItem.a)){
                    l.add(StarsItem.a);
                }
                im.setLore(l);
            }else {
                ArrayList<String> l = new ArrayList<>();
                l.add(StarsItem.a);
                im.setLore(l);
            }
            im.setUnbreakable(c.getBoolean(s + ".unbreakable"));
            im.setCustomModelData(c.getInt(s + ".model-data"));
            im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
            im.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
            im.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
            is.setItemMeta(im);
        }
        return is;
    }
    public static boolean isOk(double d){
        d = d * 100;
        return math_random_int(1,10000) <= d;
    }

    public static int math_random_int(int a, int b) {
        if (a > b) {
            int c = a;
            a = b;
            b = c;
        }
        return (int) Math.floor(Math.random() * (b - a + 1) + a);
    }
}
